Using parameters in SubReport, you can connect a SubReport to the parent report. By setting the parameter on the field that binds the parent report to SubReport, the parent report passes the data to display in SubReport through a parameter. This walkthrough illustrates the method to link the main report with a SubReport using parameters.
This walkthrough is split up into the following activities:
When you complete this walkthrough you get a layout that looks similar to the following:
To add an ActiveReport to the Visual Studio project
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the parent report (rptParent) to a data source
SQL Query |
Copy Code
|
---|---|
Select * from suppliers order by country |
To connect the child report (rptChild) to a data source using parameter
SQL Query |
Copy Code
|
---|---|
SELECT * FROM products INNER JOIN categories ON products.categoryid = categories.categoryid WHERE Products.SupplierID = <%SupplierID%> |
To create a layout for the parent report (rptParent)
Property Name | Property Value |
---|---|
Name | ghSuppliers |
DataField | Country |
Property Name | Property Value |
---|---|
DataField | Country |
Name | txtCountry |
Text | Country |
Location | 0, 0 |
Font | Name:Arial, Size:13pt, Bold:True |
TextBox1
Property Name | Property Value |
---|---|
DataField | CompanyName |
Name | txtCompanyName |
Text | Company Name |
Location | 0.0625, 0.0625 |
Size | 2.25, 0.2 in |
BackColor | Silver |
Font | Bold:True |
TextBox2
Property Name | Property Value |
---|---|
DataField | ContactName |
Name | txtContactName |
Text | Contact Name |
Location | 2.312, 0.0625 |
Size | 1.708, 0.2 in |
BackColor | Silver |
Font | Bold:True |
TextBox3
Property Name | Property Value |
---|---|
DataField | Phone |
Name | txtPhone |
Text | Phone |
Location | 4.562, 0.0625 |
Size | 1.542, 0.2 in |
BackColor | Silver |
Font | Bold:True |
SubReport
Property Name | Property Value |
---|---|
Name | Subreport1 |
ReportName | ProductName |
Location | 0.0625, 0.312 |
To create a layout for the child report (rptChild)
Property Name | Property Value |
---|---|
Name | ghProducts |
DataField | CategoryName |
Property Name | Property Value |
---|---|
DataField | CategoryName |
Name | txtCategoryName |
Text | Category Name |
Location | 0.0625, 0.0625 |
Size | 2.042, 0.2 in |
ForeColor | Maroon |
Font | Bold:True |
Property Name | Property Value |
---|---|
DataField | ProductName |
Name | txtProductName |
Text | Product Name |
Location | 0.0625, 0.0625 |
Size | 1.99, 0.2 in |
ForeColor | Red |
To connect the child report (rptChild) to the SubReport control in parent report (rptParent)
To write the code in Visual Basic
Visual Basic.NET code. Paste JUST ABOVE the ReportStart event. |
Copy Code
|
---|---|
Dim rpt As rptChild |
Visual Basic.NET code. Paste INSIDE the ReportStart event. |
Copy Code
|
---|---|
rpt = New rptChild() |
To write the code in C#
C# code. Paste JUST ABOVE the ReportStart event. |
Copy Code
|
---|---|
private rptChild rpt; |
C# code. Paste INSIDE the ReportStart event. |
Copy Code
|
---|---|
rpt = new rptChild(); |
To write the code in Visual Basic
Visual Basic.NET code. Paste INSIDE the Format event. |
Copy Code
|
---|---|
Me.SubReport1.Report = rpt |
To write the code in C#
C# code. Paste INSIDE the Format event. |
Copy Code
|
---|---|
this.subReport1.Report = rpt; |
To set the ShowParametersUI property of SubReport to False
To view the report
OR